package ru.xrm.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

import ru.xrm.selenium.model.FullFilmInfo;

public class ViewFilmInfoPage extends InternalPage {

	public ViewFilmInfoPage(PageManager pages) {
		super(pages);
	}

	@FindBy(xpath = "//*[@id='movie']/div/h2")
	private WebElement filmNameYear;
	
	@FindBy(css = "img[alt='Edit']")
	private WebElement editButton;
	
	@FindBy(css = "img[alt='Remove']")
	private WebElement removeButton;
	
	@FindBy(xpath = "//*[@id='movie']//div[@class='notes']")
	private WebElement filmNotes;

	public String getFilmNameYear() {
		return filmNameYear.getText();
	}
	
	public String getFilmNotes()
	{
		return filmNotes.getText();
	}
	
	public void clickEditButton() 
	{
		editButton.click();
	}
	
	public void clickRemoveButton() 
	{
		removeButton.click();
		wait.until(alertIsPresent()).accept();
	}
	
	public FullFilmInfo getFilmInfo()
	{
		pages.viewFilmInfoPage.ensurePageLoaded();
		FullFilmInfo film = new FullFilmInfo();
		String filmNameYearParts[] = getFilmNameYear().split(" ");
		film.setName(filmNameYearParts[0])
		.setYear(filmNameYearParts[1].replace("(", "").replace(")", ""))
		.setNotes(getFilmNotes().replace("Personal notes\n", ""));
		return film;
	}
	
	public ViewFilmInfoPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.xpath("//div[@class='content']/div[@id='movie']")));
		return this;
	}
}
